package com.zueb.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Clob;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DruidUtil {

    /**
     * 将ResultSet转换为List
     * @param rs ResultSet
     * @return List
     */
    public static List<Map<String, Object>> getHashMap(ResultSet rs) throws SQLException {
        //创建一个表
        List<Map<String, Object>> dataTable = new ArrayList<Map<String, Object>>();
        ResultSetMetaData resultSetMetaData = rs.getMetaData();
        while (rs.next()) {
            //代表  表中的一行
            Map<String, Object> item = new HashMap<String, Object>();

            //用来构造列    clob是文件类型
            for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++) {

                if (resultSetMetaData.getColumnTypeName(i).equals("CLOB")) {
                    Clob clob = rs.getClob(i);
                    if (clob == null) {
                        putToHash(item, resultSetMetaData.getColumnLabel(i).toLowerCase(), "");
                    } else {
                        putToHash(item, resultSetMetaData.getColumnLabel(i).toLowerCase(), getClobToString(rs.getClob(i)));
                    }
                } else {
                    putToHash(item, resultSetMetaData.getColumnLabel(i).toLowerCase(), rs.getString(i));
                }
            }
            dataTable.add(item);
        }
        return dataTable;
    }
    /**
     *
     * @param clob
     * @return
     * @throws SQLException
     */
    private static String getClobToString(Clob clob) throws SQLException {
        return clob.getSubString(((long)1),((int)(clob.length())));
    }
    /**
     *
     * @param item
     * @param key
     * @param value
     */
    private static void putToHash(Map<String, Object> item, String key, String value) {
        if(value!= null){
            item.put(key, value);
        }
    }


    /**
     * 读取配置文件
     * @return
     */
    public static SqlSession getSqlSession() throws IOException {

        //1、加载mybatis的核心配置文件，获取SqlSessionFactory
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2、获取SqlSession对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();

        return sqlSession;

    }

    /**
     * 关闭连接
     */
    public static void close(SqlSession sqlSession) {
        try {
            if(sqlSession != null) {
                sqlSession.close();
                sqlSession = null;
            }

        } catch (Exception e) {
            System.out.println("关闭连接失败");
            e.printStackTrace();
        }
    }
}
